/**
 * 您的公司.com
 * Copyright (c) 2018-2018 All Rights Reserved.
 */
package com.mei.demo.reflect;

/**
 * 线程安全的饿汉单例模式
 *
 * @author mei.xiaoliang@qq.com
 */
public class DemoSingleton {

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                ISingleton singleton = ISingleton.getInstance();
                singleton.print();
            }, "i:" + i).start();
        }
    }

}

class ISingleton {

    private static volatile ISingleton item = null;

    private ISingleton() {
        System.out.println("单例 ISingleton: threadName=" + Thread.currentThread().getName());
    }

    public static ISingleton getInstance() {
        if (null == item) {
            synchronized (ISingleton.class) {
                if (null == item) {
                    item = new ISingleton();
                }
            }
        }
        return item;
    }

    public void print() {
        System.out.println("123123");
    }

}